package edu.cuit.zhuyimeng.ticket.convert;

import cn.hutool.extra.spring.SpringUtil;
import edu.cuit.zhuyimeng.flight.api.controller.FlightQueryApiController;
import edu.cuit.zhuyimeng.flight.api.dto.FlightInfoDTO;
import edu.cuit.zhuyimeng.framework.common.exception.QueryException;
import edu.cuit.zhuyimeng.framework.common.result.CommonResult;
import edu.cuit.zhuyimeng.ticket.controller.vo.TicketInfoVO;
import edu.cuit.zhuyimeng.ticket.dal.po.TicketPO;
import lombok.extern.slf4j.Slf4j;

/**
 * 飞机票转换器
 */
@Slf4j
public class TicketConverter {

    /**
     * TicketPO转换TicketInfoVO
     * @param ticketPO 飞机票PO
     * @return 飞机票信息VO
     */
    public static TicketInfoVO convertTicketInfoVO(TicketPO ticketPO) {
        FlightQueryApiController queryApiController = SpringUtil.getBean(FlightQueryApiController.class);
        CommonResult<FlightInfoDTO> flightInfoDTOResult = queryApiController.queryOneFlight(ticketPO.getFlightId());
        if (flightInfoDTOResult.isError()) {
            log.error("查询FlightInfoDTO失败: " + flightInfoDTOResult.getMsg());
            throw new QueryException(flightInfoDTOResult.getMsg());
        }
        return TicketInfoVO.builder()
                .flightInfo(flightInfoDTOResult.getData())
                .id(ticketPO.getId())
                .userLoginId(ticketPO.getUserLoginId())
                .build();
    }

}
